<?php


namespace app\admin\controller\users;

use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\User as UserModel;
use app\admin\model\Third;
use app\admin\model\WhiteList as WhiteListModel;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class White extends Backend
{
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('white_list');
    }

    /**
     * 列表
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            if ($total > 0) {
                foreach ($list as $key => $val) {
                    if ($val['start_time'] && $val['end_time']) {
                        $start_time = date('Y-m-d H:i:s', $val['start_time']);
                        $end_time = date('Y-m-d H:i:s', $val['end_time']);
                        $list[$key]['youxiaoqi'] = $start_time . '----' . $end_time;
                    }
                }
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if ($params['start_time'] && $params['end_time']) {
                    $params['start_time'] = strtotime($params['start_time']);
                    $params['end_time'] = strtotime($params['end_time']);
                }

                $info = $this->model->where(['mobile' => $params['mobile']])->find();
                if ($info) {
                    $this->error('手机号已经存在');
                }
                $params['add_time'] = time();
                $res = $this->model->insertGetId($params);
                if ($res) {
                    $this->success('操作成功');
                }

                $this->error('操作失败');
            }
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($row['start_time'] && $row['end_time']) {
            $row['start_time'] = date('Y-m-d H:i:s', $row['start_time']);
            $row['end_time'] = date('Y-m-d H:i:s', $row['end_time']);
        } else {
            $row['start_time'] = '';
            $row['end_time'] = '';
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $res = $row->save($params);
                if ($res) {
                    $this->success('操作成功');
                }
                $this->error('操作失败');
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}